# -*- coding: utf-8 -*-
from settings import OPENSTACK_ADMIN_USER
from settings import OPENSTACK_ADMIN_PASS
from settings import OPENSTACK_ADMIN_TENANT
from settings import OPENSTACK_AUTH_URL
import ceilometerclient.client
import time,datetime
import decimal
from decimal import Decimal

class Ceilometer():
    
    def client(self):
        username = OPENSTACK_ADMIN_USER
        password = OPENSTACK_ADMIN_PASS
        project_name = OPENSTACK_ADMIN_TENANT
        auth_url = OPENSTACK_AUTH_URL
        version = '2'
        cclient = ceilometerclient.client.get_client(version, os_username=username, os_password=password, os_tenant_name=project_name, os_auth_url=auth_url)
        return cclient
    
    def getVcpuUseData(self,cclient,timestamp,period):
        return cclient.statistics.list('vcpus',q=[{"field": "timestamp", "op": "ge", "value":timestamp}],period=period,groupby='project_id')
    
    def getDiskUseData(self,cclient,timestamp,period):
        return cclient.statistics.list('disk.root.size',q=[{"field": "timestamp", "op": "ge", "value":timestamp}],period=period,groupby='project_id')
    
    def getHardDiskUseData(self,cclient,timestamp,period):
        return cclient.statistics.list('disk.ephemeral.size',q=[{"field": "timestamp", "op": "ge", "value":timestamp}],period=period,groupby='project_id')
    
    def getMemoryUseData(self,cclient,timestamp,period):
        return cclient.statistics.list('memory',q=[{"field": "timestamp", "op": "ge", "value":timestamp}],period=period,groupby='project_id')
    
    def getHourUseDataByProject(self,cclient,period,timestamp,project_id):
        cpusList = cclient.statistics.list('vcpus',q=[{"field": "timestamp", "op": "ge", "value":timestamp},{"field":"project_id","op":"eq","value":project_id}],period=period,groupby='project_id')
        diskList = cclient.statistics.list('disk.root.size',q=[{"field": "timestamp", "op": "ge", "value":timestamp},{"field":"project_id","op":"eq","value":project_id}],period=period,groupby='project_id')
        memoryList = cclient.statistics.list('memory',q=[{"field": "timestamp", "op": "ge", "value":timestamp},{"field":"project_id","op":"eq","value":project_id}],period=period,groupby='project_id')
        try:
            cpu_use = cpusList[0].sum
            memory_use = memoryList[0].sum
            disk_use = diskList[0].sum
            cpu_unit = cpusList[0].unit
            disk_unit = diskList[0].unit
            memory_unit = memoryList[0].unit
        except:
            cpu_use = 0
            disk_use = 0
            memory_use = 0
            cpu_unit = u'vcpu'
            disk_unit = u'GB'
            memory_unit = u'GB'
        return {"project_id":project_id,"cpu_unit":cpu_unit,"disk_unit":disk_unit,"memory_unit":memory_unit,"cpu_use":cpu_use,"disk_use":disk_use,"memory_use":Decimal(memory_use/1024).quantize(Decimal('0.00'))}
    
    if __name__ == '__main__':
        
        now = datetime.datetime.now()
        last_time = now + datetime.timedelta(hours=-9)
        cclient =  client()
        #statisticList =  cclient.statistics.list('disk.root.size',q=[{"field": "timestamp", "op": "ge", "value":last_time},{"field":"project_id","op":"eq","value":"5e779a87f3934d9da2fbc94bc0fd35b0"}],period=60*60,groupby='project_id')
        result = getHourUseDataByProject(cclient,60*60,last_time,"a375a60cc3f644cf906f337dcb7d4349")
        
        print result
        
        #for index in range(len(statisticList)):
        #    print statisticList[index]
